export default {
	buttonStyle: {
		backgroundImage: "linear-gradient(90deg, #ff80a8, #FE65A4)",
		color: "#FFF",
		fontSize: "32rpx",
		height: "80rpx",
		border:"0",
		"border-radius": "80rpx",
	},
	toast: function(text, duration, success) {
		uni.showToast({
			title: text,
			icon: success ? 'success' : 'none',
			duration: duration || 2000,
			position: "top"
		})
	},
	getRole: function() {
		try {
			const value = getApp().globalData.PersonInfo.setting.role;
			if (!value)
				value = 0;
			return value;
		} catch (e) {return 0}
	},
	getRoleName: function(val) {
		switch (val){
			case 1:
				return "备孕"
				break;
			case 2:
				return "已孕"
				break;
			case 3:
				return "育儿"
				break;
			default:
				return ""
				break;
		}
	},
	//显示引导页
	showGuidePage: function(){
		try {
			const value = uni.getStorageSync('EYT_LaunchFlag');
			if (value) {
				if (value == true) 
					return;
			} else {
				uni.redirectTo({
					url: '../platforms/app-plus/eyunt/guide'
				});
			}
		} catch(e) {}
	},
	//获取URL参数
	getQueryString(name) {
		var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
		var r = window.location.search.substr(1).match(reg);
		if (r != null) {
			console.log(decodeURI(r[2]))
			//return unescape(r[2]);
			return r[2];
		}
		return null;
	},
	//检测用户是否已登录
	checkPerson() {
		let per = getApp().globalData.PersonInfo;
		if (per===undefined || per===null || per._id === undefined) {
			uni.navigateTo({
				url: "/pages/login/login"
			});
			return false;
		}
		return true;
	},
	//检测用户角色
	checkRole() {
		let per = getApp().globalData.PersonInfo;
		if (!(per && per.setting)) 
			return false;
		const value = per.setting.role;
		if (value===undefined || value===null || [1,2,3].indexOf(value)<0) {
			let sURL = "/pages/mine/setRole";
			uni.navigateTo({
				url: sURL
			});
			return false;
		}
		return true;
	},
	//验证手机号
	checkPhone(sPhone){ 
		var pattern = /^1[34578]\d{9}$/;
		if (pattern.test(sPhone)) {
			return true;
		}
		return false;
	},
	formatDate(time,format='YY-MM-DD'){
		if (time === undefined)
			var date = new Date();
		else
			var date = new Date(time);
	 
	    var year = date.getFullYear(),
	        month = date.getMonth()+1,//月份是从0开始的
	        day = date.getDate();

	    var preArr = Array.apply(null,Array(10)).map(function(elem, index) {
	        return '0'+index;
	    });//开个长度为10的数组 格式为 ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09"]
	 
	    var newTime = format.replace(/YY/g,year)
	        .replace(/MM/g,preArr[month]||month)
	        .replace(/DD/g,preArr[day]||day)
	 
	    return newTime;
	},
	formatDatetime(time,format='YY-MM-DD hh:mm:ss'){
		if (time === undefined)
			var date = new Date();
		else
			var date = new Date(time);
	 
	    var year = date.getFullYear(),
	        month = date.getMonth()+1,//月份是从0开始的
	        day = date.getDate(),
	        hour = date.getHours(),
	        min = date.getMinutes(),
	        sec = date.getSeconds();
	    var preArr = Array.apply(null,Array(10)).map(function(elem, index) {
	        return '0'+index;
	    });//开个长度为10的数组 格式为 ["00", "01", "02", "03", "04", "05", "06", "07", "08", "09"]
	 
	    var newTime = format.replace(/YY/g,year)
	        .replace(/MM/g,preArr[month]||month)
	        .replace(/DD/g,preArr[day]||day)
	        .replace(/hh/g,preArr[hour]||hour)
	        .replace(/mm/g,preArr[min]||min)
	        .replace(/ss/g,preArr[sec]||sec);
	 
	    return newTime;
	},
	toDate(dt){
		if (dt) {
			if (dt instanceof Date)
				return dt;
			if (typeof dt == "number") {
				if (dt.toString().length == 10) 
					dt *= 1000;
			}
			return new Date(dt)
		}
		else 
			return new Date();
	},
	Today(){
		var today = new Date();
		var todayStr = today.getFullYear()+"-" + (today.getMonth()+1) + "-" + today.getDate();
		return this.toDate(todayStr);
	},
	//时间 -> 时间戳
	timeToTimestamp(time) {
		var date = this.toDate(time)
		
		return parseInt(date.getTime()/1000)
	},
	incYears(nYears){
		var date = new Date();
		date.setYear(date.getFullYear() + nYears);
		return date;
	},
	incDays(nDays, dt){
		var date = this.toDate(dt);
		date.setDate(date.getDate() + nDays);
		return date;
	},
	//计算孕周
	calcYunzhou(ycq, dt) {
		var curyunzhou = {week:0, day:0};
		var date1 = this.toDate(ycq);
			
		var date2 = this.toDate(dt).getTime();
		let date3 = 40*7*24*60*60*1000;
		let fortyago = date1 - date3;
		let yunzhou=parseInt(Math.floor(Math.abs(date2 - fortyago)/(1000*60*60*24)));
		curyunzhou.week = parseInt(yunzhou/7) ;
		curyunzhou.day = yunzhou % 7 ;
		
		/*let dates = Math.abs(date1 - date2)/(1000*60*60*24);
		curyunzhou.week = parseInt(dates/7) ;
		curyunzhou.day = dates % 7 ;*/
		
		return curyunzhou;
	},
	formatYunzhou(ycq, dt, code) {
		let yz = this.calcYunzhou(ycq, dt);
		if (yz === undefined || yz === null)
			return ""
		else {
			let sRes = yz.week.toString()
			if (yz.day > 0) {
				if (!code)
					sRes += "<sup>+" + yz.day.toString() + "</sup>";
				else
					sRes += "+" + yz.day.toString() + "天";
			}
			else {
				sRes += "周"
			}
				
			return sRes;
		}
		
	},
	formatYunzhou(fYZ, code) {
		let sRes = ""
		let week = 0;
		let day = 0;
		if (typeof fYZ === 'number' && !isNaN(fYZ)) {
			week = parseInt(fYZ);
			day = parseInt((fYZ-week) * 10);
			sRes = week.toString();
			if (day>0) {
				if (code)
					sRes += "<sup>+" + day.toString() + "</sup>"; 
				else
					sRes += "+" + day.toString() + "天";
			}
			return sRes;
		}
	},
	daysBetween(sDate1, sDate2) {
		var dateSpan, tempDate,	iDays;
		sDate1 = this.toDate(sDate1);
		sDate2 = this.toDate(sDate2);
		
		dateSpan = sDate2 - sDate1;
		dateSpan = Math.abs(dateSpan);
		iDays = Math.ceil(dateSpan / (24 * 3600 * 1000));
		return iDays
	},
	monthsBetween(sDate1, sDate2) {
		//var nDays = this.daysBetween(sDate1, sDate2);
		var date1 = this.toDate(sDate1);
		var date2 = this.toDate(sDate2);
		
		var year1 = date1.getFullYear();
		var year2 = date2.getFullYear();
		
		var month1 = date1.getMonth()
		var month2 = date2.getMonth()
		
		if (date1>date2) 
			return (year1-year2) * 12 + (month1-month2)
		else
			return (year2-year1) * 12 + (month2-month1)
	},
	yearsBetween(sDate1, sDate2) {
		/*var date1 = this.toDate(sDate1);
		var date2 = this.toDate(sDate2);
		
		return Math.abs(date1.getFullYear()-date2.getFullYear());*/
		var nMonths = this.monthsBetween(sDate1, sDate2)
		return Math.floor(nMonths / 12);
	},
	getAge(beginStr, endStr) {
		var nDays = this.daysBetween(beginStr, endStr);
		var nMonths = this.monthsBetween(beginStr, endStr);
		var nYears = this.yearsBetween(beginStr, endStr);
		if (nDays<31)
			return nDays+"天"
		else if (nMonths<12)
			return nDays+"月"
		else {
			if (nYears<=3) 
				return nYears + "岁" + nMonths%12 + "月"
			else
				return nYears;
		}

		/*
		var reg = new RegExp(/^(\d{1,4})(-|\/)(\d{1,2})\2(\d{1,2})$/);
		var beginArr = beginStr.match(reg);
		
		var bNow = (endStr===undefined || endStr==="");
		if (bNow) 
			endStr = this.formatDate(undefined, "YY-MM-DD");
			
		
		var endArr = endStr.match(reg);
		var days = 0;
		var month = 0;
		var year = 0;
		days = endArr[4] - beginArr[4];
		if (days < 0) {
			month = -1;
			days = 30 + days;
		}

		month = month + (endArr[3] - beginArr[3]);
		if (month < 0) {
			year = -1;
			month = 12 + month;
		}

		year = year + (endArr[1] - beginArr[1]);

		var yearString = year > 0 ? year + "岁" : "";
		var mnthString = month > 0 ? month + "月" : "";
		var dayString = days > 0 ? days + "天" : "";

		var result = "";
		if (year >= 7) 
		{
			result = year + "岁";
		} 
		else 
		{
			if (year >= 1) 
			{
				result = yearString + mnthString;
			}
			else 
			{
				if (month >= 1) 
				{
					result = days > 0 ? mnthString + dayString : mnthString;
				} 
				else 
				{
					var sHour = beginArr[6];
					if (!sHour)
						sHour = "0";
					var begDate = new Date(beginArr[1], beginArr[3] - 1,
							beginArr[4], sHour, "0", "0");
							
					sHour = "0"
					if (bNow && !endArr[6]) {
						var date = new Date();
						sHour = date.getHours();
					}
					var endDate = new Date(endArr[1], endArr[3] - 1, endArr[4],
							sHour, "0", "0");

					var between = (endDate.getTime() - begDate.getTime()) / 1000;
					days = Math.floor(between / (24 * 3600));
					var hours = Math.floor(between / 3600 - (days * 24));
					var dayString = days > 0 ? days + "天" : "";
					result = days >= 3 ? dayString : days * 24 + hours + "小时";
				}
			}
		}

		return result;
		*/
	},
	//下载行政区划
	downAreas(nUptime) {
		// 本地没有缓存 需要下载
		let _this = this;
		let sURL = getApp().globalData.ServerURL("api/dictdata");
		uni.request({
			url: sURL,
			method: "POST",
			//responseType: "arraybuffer",
			data: {
				type: ["area"],
				update_time: nUptime
			},
			success: (res) => {
				if (res.statusCode === 200 && res.data.data !== undefined) {
					uni.setStorage({
						key: "EYT_JSON_AREA",
						data: res.data
					});
				}
			}
		}); 
	},
	checkAreas() {
		let _this = this;
		uni.getStorage({
			key: "EYT_JSON_AREA",
			fail:function(res) {
				// 本地没有缓存 需要下载
				_this.downAreas(0);
			}
		});
	}
	
	
}